#ifndef SPIN_LOCK_H
#define SPIN_LOCK_H

#include <atomic>

class spin_lock{

public:
spin_lock(){
    this->flag=false;
}
    void lock(){
        bool expected=false;
        while(!flag.compare_exchange_strong(expected,true)){
            //如果flag值与expected不相等,就会将expected设置成当前的flag,为了实现等待，那么置为false
            expected=false;
        }

        
    }

    void unlock(){
        flag=false;
    }
private:

    std::atomic<bool> flag;
    


};

#endif
